import battlegui.*;
import java.util.HashMap;
import java.util.ArrayList;
/**
 * The project "Strijd der Galliers" is a game where you need to burn down every house of the other
 * player. 
 *
 *The class BattleGround makes a grid where the player will need to put his sheds. This grid
 *will allow you to know the sheds that are touched and the shots the player missed.
 *
 *@author Jonathan Smet & Mathieu Reymond
 * @version 12.03.11
 */
public class BattleGround
{
    private ArrayList<String[]> battleGrid;
    private HashMap<String, Shed> battleGridKey;
    private int size1ShedsLeft;
    private int size2ShedsLeft;
    private int gridWidth;
    private int gridHeight;
    private ArrayList<Shed> shedList;

    /**
     * Makes a class BattleGround.
     * 
     * @param int The grid's width
     * @param int The grid's Height
     */
    public BattleGround(int gridWidth,int gridHeight)
    {
        this.gridWidth = gridWidth;
        this.gridHeight = gridHeight;
        shedList = new ArrayList<Shed>();
        battleGridKey = new HashMap<String, Shed>();
        battleGrid = new ArrayList<String[]>();
        size1ShedsLeft = howManySheds();
        size2ShedsLeft = howManySheds();
        setBattleGround();
    }

    /**
     * This method allows you to put your sheds on the grid.
     * 
     * @param  Sched   The shed you want to place
     * @param Position The position of the shed
     */
    public String placeShed(Shed shedToPlace, Position shedPosition)
    {
        String returnString;
        int shedsLeft;
        int shedSize = shedToPlace.getSize();
        boolean allowShed = true;
        int shedNumber = howManySheds();
        int startX = shedPosition.getX();
        int startY = shedPosition.getY();
        int endX = startX + 1;
        int endY = startY + 1;
        if(shedToPlace.getOrientation() == "VERTICAL")
        {
            endY = startY + shedSize;
        }
        else
        {
            endX = startX + shedSize;
        }
        
        
        if(shedToPlace.getSize() == 2)
        {
            shedsLeft = size1ShedsLeft;
        }
        else
        {
            shedsLeft = size2ShedsLeft;
        }
        if(endX > gridWidth || endY > gridHeight)
        {
            endX = gridWidth;
            endY = gridHeight;
            shedsLeft = 0;                    //Check if the shed is entirely in the grid
        }
        
        for(int xIndex = startX; xIndex < endX; xIndex++)
        {
            for(int yIndex = startY; yIndex < endY; yIndex++)
            {
                if(battleGrid.get(xIndex)[yIndex] != null || shedsLeft == 0)//If there is a non-null value in the grid where you want to
                {                                                           //place your shed then you won't be allowed to place it.
                    allowShed = false;
                }
            }
        }
        
        if(allowShed == true)
        {
            for(int xIndex = startX; xIndex < endX; xIndex++)
            {
                for(int yIndex = startY; yIndex < endY; yIndex++)                                   //Fill the grid with  
                {                                                                                   //the name of your shed    
                    String[] newRow = battleGrid.get(xIndex);
                    String stringInput = Integer.toString(shedsLeft) + Integer.toString(shedSize);
                    newRow[yIndex] = stringInput;
                    battleGridKey.put(stringInput, shedToPlace); //Links the shed's name with the real shed
                }
            }
            
            shedList.add(shedToPlace);
            returnString = ("Your shed is succesfully placed !");               
            if(shedToPlace.getSize() == 2)
            {
                size1ShedsLeft--;
            }
            else
            {
                size2ShedsLeft--;
            }
        }
        else
        {
            returnString = ("You are not allowed to place a shed with :\n - orientation : " + shedToPlace.getOrientation() + "\n - size : " + Integer.toString(shedSize));
        }
        return returnString;
    }
    
    /**
     * This method allows you to create a battleground with chosen dimensions.
     */
    public void setBattleGround()
    {      
       for(int index = 0; index < gridWidth; index++)
       {
           battleGrid.add(new String[gridHeight]); //makes a grid with size height*width
       }
    }
    
    /**
     * This method allows you to shoot on the grid and see wether a shed is touched or not.
     * 
     * @param Position The position (coordinates) of your shot.
     * @return Two booleans : the first one to know if you are allowed to shoot there or not, the second one to know if you touched a shed or not.
     */
    public boolean[] shot(Position shotPosition)
    {
        int x = shotPosition.getX();
        int y = shotPosition.getY();
        String positionValue = battleGrid.get(x)[y];
        boolean[] result = new boolean[2];
        if(positionValue == "shotMissed" || positionValue == "shotTouched")
        {
            result[0] = false;                                //Let you know if you already hit this place
        }
        else
        {
            result[0] = true;
            if(positionValue == null)
            {
                result[1] = false;
                String[] newValue = battleGrid.get(x);      
                newValue[y] = "shotMissed";
            }
            else
            {
                result[1] = true;
                String[] newValue = battleGrid.get(x);
                newValue[y] = "shotTouched";
                Shed shedTouched = battleGridKey.get(positionValue);
                shedTouched.changeShedParts();
            }
        }
        return result;
    }
    
    /**
     * This method allows you to see if a shed is totally touched or not.
     * 
     * @param Shed The shed that must be checked
     * @return True if it is entirely touched, false if not
     */
    public boolean checkShed(Shed checkThisShed)
    {
        boolean shedDown = true;
        ArrayList<Boolean> shedParts = checkThisShed.getShedParts();
        
        for(int index = 0; index < shedParts.size(); index++)
        {
            if(shedParts.get(index) == true)
            {
                shedDown = false;
            }
        }
        return shedDown;
    }
    
    /**
     * This method let you to know how many sheds you are allowed to place at the beginning
     * of the game.
     * 
     * @return The number of each type of sheds you are allowed to place
     */
    public int howManySheds()
    {
       int gridArea = gridWidth*gridHeight;
       return gridArea/24;
      
           }
    
    /**
     * This method let you know if you win or not the game.
     * 
     * @return True if you win, false if not
     */
    public boolean victoryCondition()
    {
        boolean victoryCondition = true;
        for(int index = 0; index < shedList.size(); index++)
        {
            boolean shedResult = checkShed(shedList.get(index));
            if(shedResult == false)
            {
                victoryCondition = false;
            }
            
        }
        return victoryCondition;
    }
}
